package com.wechat.shopping.mall.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 进制转换工具
 *
 */
public class NumberSystemUtils {

	public final static Logger logger = LoggerFactory.getLogger(NumberSystemUtils.class);

	private final static String DIGIT = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/**
	 * 10进制转换成36进制
	 * @param val 十进制
	 * @return 
	 */
	public static String ConvertTo36(String decimalismString) {
		return ConvertTo36(Integer.valueOf(decimalismString));
	}
	/**
	 * 10进制转换成36进制
	 * @param val 十进制
	 * @return 
	 */
	public static String ConvertTo36(int decimalism) {
		String result = "";
		while (decimalism >= 36) {
			result = DIGIT.charAt(decimalism % 36) + result;
			decimalism /= 36;
		}
		if (decimalism >= 0){
			result = DIGIT.charAt(decimalism) + result;
		}
		return result;
	}

	/**
	 * 36进制转换成10进制
	 * @param str 36进制字符串
	 * @return
	 */
	public static int ConvertTo10(String system36String) {
		int result = 0;
		int len = system36String.length();
		for (int i = len; i > 0; i--) {
			result += DIGIT.indexOf(system36String.charAt(i - 1)) * new Double((Math.pow(36, len - i))).intValue();
		}
		return result;
	}

	/**
	 * 36进制path转10进制groupId字符串
	 * ex: convertToPath10("/1/10/1B/") = [1,36,47]
	 * ex: convertToPath10(null) = []
	 * @param path36
	 * @return
	 */
	public static List<Integer> convertToPath10(String path36){
		List<Integer> groupIdList = new ArrayList<Integer>();
		if(null==path36||path36.equals("/")){
			return groupIdList;
		}
		try {
			String[] paths = path36.substring(1).split("/");
			for (int i = 0; i < paths.length; i++) {
				groupIdList.add(NumberSystemUtils.ConvertTo10(paths[i]));
			}
		} catch (Exception e) {
			logger.error("转换异常", e);
		}
		return groupIdList;
	}

	/**
	 * 36进制path转10进制,并返回指定层级index。
	 * ex: convertToPath10("/1/10/1B/", 1) = 36
	 * ex: convertToPath10("/1/10/1B/", 3) = null
	 * ex: convertToPath10(null, 3) = null
	 * @param path36
	 * @param index
	 * @return
	 */
	public static Integer convertToPath10ByIndex(String path36, int index){
		Integer result = null;
		if(StringUtils.isBlank(path36)){
			return result;
		}
		try {
			String[] paths = path36.substring(1).split("/");
			if(index<paths.length){
				result = NumberSystemUtils.ConvertTo10(paths[index]);
			}
		} catch (Exception e) {
			logger.error("转换异常", e);
		}
		return result;
	}

	/**
	 * 36进制path转10进制,并返回当前id层级
	 * ex: getLevelByPath("/1/A/1B/", 1) = 0
	 * ex: getLevelByPath("/1/A/1B/", 36) = 10
	 * ex: getLevelByPath(null, 3) = null
	 * @param path36
	 * @param id
	 * @return
	 */
	public static Integer getLevelByPath(String path36,Integer id){
		Integer result = null;
		if(StringUtils.isBlank(path36) || null == id){
			return result;
		}
		try {
            String id36 = NumberSystemUtils.ConvertTo36(id);
			String[] paths = path36.substring(1).split("/");
            for (int index=0;index < paths.length;index++){
                if(id36.equals(paths[index])){
                    result = index;
                    break;
                }
            }
		} catch (Exception e) {
			logger.error("转换异常", e);
		}
		return result;
	}
	
	/**
	 * 36进制path转10进制,并返回最后的id。
	 * ex: convertToPath10("/1/10/") = 36
	 * ex: convertToPath10(null) = null
	 * @param path36
	 * @param index
	 * @return
	 */
	public static Integer convertToPath10AtLast(String path36){
		Integer result = null;
		if(StringUtils.isBlank(path36)){
			return result;
		}
		try {
			String[] paths = path36.substring(1).split("/");
			result = NumberSystemUtils.ConvertTo10(paths[paths.length-1]);
		} catch (Exception e) {
			logger.error("转换异常", e);
		}
		return result;
	}
	
	/**
	 * 十进制的path转int的List
	 * ex: convertToList("/1/10/123/") = [1,10,123]
	 * ex: convertToList(null) = []
	 * @param path36
	 * @return
	 */
	public static List<Integer> convertToList(String path){
		List<Integer> groupIdList = new ArrayList<Integer>();
		if(null==path){
			return groupIdList;
		}
		try {
			String[] paths = path.substring(1).split("/");
			for (int i = 0; i < paths.length; i++) {
				groupIdList.add(Integer.valueOf(paths[i]));
			}
		} catch (Exception e) {
			logger.error("转换异常", e);
		}
		return groupIdList;
	}
	
	/**
	 * 产生一个0到制定范围的随机整数
	 * @param range
	 * @return
	 */
	public static int getRandomInt(int range){
		Random random = new Random(range);
		return random.nextInt();
	}
	
	/**
     * 获取path的层级
     * ex: getLevelByPath("/1/A/1B/") = 3
     * ex: getLevelByPath("/1/A/1B/11B/12B/") = 5
     * ex: getLevelByPath(null) = -1
     * @param path
     * @return
     */
    public static Integer getLevelByPath(String path){
        Integer result = -1;
        if(StringUtils.isBlank(path)){
            return result;
        }
        try {
            String[] paths = path.substring(1).split("/");
            result = paths.length;
        } catch (Exception e) {
            logger.error("转换异常", e);
        }
        return result;
    }
}
